<template>
  <div class="search-box">
    <div class="row">
      <div class="left">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="queryRoleList">查询搜索</el-button>
      </div>
    </div>
    <div class="row">
      <span>角色名称:</span>
      <el-input
        v-model="roleListParams.keyword"
        placeholder="请输入角色名称"
      ></el-input>
    </div>
  </div>
  <div class="add-area">
    <el-button type="primary" @click="showDialog = true">添加</el-button>
  </div>
  <el-table :data="roleList" border>
    <el-table-column label="编号" prop="id" />
    <el-table-column label="角色名称" prop="name" />
    <el-table-column label="描述" prop="description" />
    <el-table-column label="用户数" prop="adminCount" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        {{ formateDate(row.createTime) }}
      </template>
    </el-table-column>
    <el-table-column label="是否启用" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'allocMenu'"
          @click="router.push('/ums/allocMenu?id=' + row.id)"
          >分配菜单</el-button
        >
        <el-button
          text
          type="primary"
          v-permission="'allocResource'"
          @click="router.push('/ums/allocResource?id=' + row.id)"
          >分配资源</el-button
        >
        <el-button
          text
          type="primary"
          @click="editRole(row.id, row.name, row.description, row.status)"
          v-permission="'editRole'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <el-pagination
      v-model:current-page="roleListParams.pageNum"
      v-model:page-size="roleListParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="queryRoleList"
      @current-change="queryRoleList"
    />
  </ElConfigProvider>
  <Roleedit :show="showDialog" @hide="closeDialog" :id="editId" :data="data" />
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { roleListApi, switchRoleStatusApi } from "./api";
import { Search } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { formateDate } from "@/utils";
import Roleedit from "./editrole.vue";
import { useRouter } from "vue-router";

const router = useRouter();

const roleList = ref<IRoleModel[]>([]);
const total = ref(0);
const showDialog = ref(false);
const editId = ref(0);

// 编辑传过去的值
// const name = ref("");
// const description = ref("");
const data = ref({});

const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  editId.value = 0;
  if (isReload) queryRoleList();
};

const roleListParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});

const editRole = (
  id1: number,
  name1: string,
  description1: string,
  status1: string
) => {
  showDialog.value = true;
  editId.value = id1;
  data.value = {
    name: name1,
    description: description1,
    id: id1,
    status: status1,
  };
};

const switchStatus = (row: IUserModel) => {
  switchRoleStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};

const reset = () => {
  (roleListParams.pageNum = 1),
    (roleListParams.pageSize = 10),
    (roleListParams.keyword = "");
  queryRoleList();
};

const queryRoleList = () => {
  roleListApi(roleListParams).then((res) => {
    if (res.code === 200) {
      roleList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};

queryRoleList();
</script>
<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center !important;
  }
}
.search-box {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  .row {
    padding: 20px 0;
    display: flex;
    &:first-child {
      justify-content: space-between;
    }
    &:last-child {
      width: 400px;
      span {
        width: 100px;
      }
    }
  }
}
.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}

.add-area {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
  margin-bottom: 20px;
}
</style>
